package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.pojos.WmNews;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @author lzy
 * @date 2022/7/23 17:07
 */
//监听器
@Component
public class ArticleDownListener {
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = "news.down.up.topic")
    public void consumer(String message) {
        if (StringUtils.isBlank(message)) {
            return;
        }
        //将JSON转化成WmNews类
        WmNews wmNews = JSON.parseObject(message, WmNews.class);
        if (wmNews == null) {
            return;
        }
        Long articleId = wmNews.getArticleId();
        ApArticleConfig configResult = apArticleConfigMapper.selectById(articleId);
        if (configResult == null) {
            return;
        }
        //获取新闻监听状态 设置为0（下架状态）
        Short enable = wmNews.getEnable();
        configResult.setIsDown(enable == 0);
        apArticleConfigMapper.updateById(configResult);
    }

}
